home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_100 / 128_01 / roff4161.c < prev    next >
Text File  |  1985-03-09  |  13KB  |  499 lines

  1. /********************************************************/
  2. /*                            */
  3. /*            ROFF4, Version 1.61            */
  4. /*                            */
  5. /*(C) 1983,4 by Ernest E. Bergmann            */
  6. /*        Physics, Building #16            */
  7. /*        Lehigh Univerisity            */
  8. /*        Bethlehem, Pa. 18015            */
  9. /*                            */
  10. /* Permission is hereby granted for all commercial and    */
  11. /* non-commercial reproduction and distribution of this    */
  12. /* material provided this notice is included.        */
  13. /*                            */
  14. /********************************************************/
  15. #include "roff4.h"
  16. /*****************MAIN************MAIN*********/
  17. main(argc, argv)
  18. int argc;
  19. char **argv;
  20. {char option,*macq(),*pc;
  21. char filename[20];
  22. struct divfd *pd;
  23. dioinit (&argc, argv);    /* directed I/O */
  24. fprintf(STDERR,"ROFF4, version 1.61, Aug 23, 1984\n");
  25. fprintf(STDERR,"        (c) 1983,4 by\n");
  26. fprintf(STDERR,"E. E. Bergmann, Physics,Bldg. #16\n");
  27. fprintf(STDERR,"Lehigh University, Bethlehem PA 18015\n");
  28. SUPPRESS= debug = FALSE;
  29. if(FALSE) putchar('\n');/*for linkage purposes only!*/
  30. if (argc == 1)
  31.       { fprintf(STDERR,"USAGE:  ROFF4 file1\n  more than one file OK\n");
  32.     exit();
  33.       }
  34. argv++;
  35. init_defaults();
  36. while ( --argc > 0 )
  37.       { strcpy (filename, *argv++);
  38.     fprintf(STDERR,"Now processing <%s>\n",filename );
  39.     if(filename[0]=='-')
  40.         {option=filename[1];
  41.         if(option=='M') showm();
  42.         else if(option=='B') debug=TRUE;
  43.         else if(option=='D') showd();
  44.         else if(option=='F') putchar(FORMF);
  45.         else if(option=='G') gloss();
  46.         else if(option=='I') showit();
  47.         else if(option=='R') showr();
  48.         else if(option=='S') PAGESTOP=TRUE;
  49.         else if(option=='O') range(&filename[2]);
  50.         else    {KEYBD=option;
  51.             dolns();
  52.             fprintf(STDERR,"End of keyboard input <%c>\n",
  53.                 KEYBD);
  54.             KEYBD=FALSE;
  55.             }
  56.         continue;
  57.         }
  58.     if(pd=find2(filename,DLINK)) dclose(pd);
  59.     if (ERROR == fopen( filename,IOBUF))
  60.         {fprintf(STDERR,"can't open <%s>\n",filename );
  61.         continue;
  62.         }
  63.     else dolns();
  64.     if(VLINENO>0||OUTBUF[0]) space(HUGE);
  65.     fprintf(STDERR,"Done processing <%s>\n", filename );
  66.     fclose(IOBUF);
  67.       }     /* end while (--argc > 0 ) */
  68. dsclose();
  69. dioflush();
  70. }        /* end main()           */
  71. /****************************************/
  72. dolns()    /*do processing of lines*/
  73. {char *pc;
  74. int typ;
  75. int e;
  76.   BINP=0;
  77.    while((e=fgets2(LINE,IOBUF))||FPTR) /*until EOF or CPMEOF */
  78.       {if(e) {if(LINE[0]==COMMAND)
  79.         {if(pc=macq(LINE)) pbstr(pc);
  80.         else comand(LINE);
  81.         }
  82.           else text(LINE);
  83.          }
  84.     else endso();
  85.       }
  86. }
  87. /**************************************************************
  88. initializes the global variables governing the execution of the
  89.  format commands.
  90. **************************************************************/
  91. init_defaults()
  92. {initsk(&FILL,FI_DEF);    /* yes we want filled lines */
  93. initsk(&LSVAL,LS_DEF);    /* line spacing = 1 */
  94. initsk(&INVAL,IN_DEF);    /* left margin indent  0 */
  95. initsk(&RMVAL,RM_DEF);    /* right margin = page width  */
  96. TIVAL = TI_DEF;    /* left margin temporary indent    0 */
  97. CEVAL = 0;    /* next n lines to be centered -  0  */
  98. PAGESTOP = FALSE;
  99. FFEED = FF_INI;
  100. FIRSTPAGE=1;
  101. LASTPAGE=30000;/*infinite*/
  102. JUSTIFY=JU_INI;
  103. initsk(&PLVAL,PL_DEF);
  104. initsk(&M1VAL,M1_DEF);
  105. initsk(&M2VAL,M2_DEF);
  106. initsk(&M3VAL,M3_DEF);
  107. initsk(&M4VAL,M4_DEF);
  108. initsk(&SCVAL,SC_INI);
  109. initsk(&OWVAL,OW_INI);
  110. initsk(&TABSIZ,TS_DEF);
  111. initsk(&TCVAL,TC_DEF);
  112. initsk(&CFVAL,CF_DEF);
  113. initsk(&ICVAL,IC_DEF);
  114. CURPAG = 0;
  115. NEWPAG = 1;
  116. FRQ=FRSTRING=WHSTRING=0;
  117. FRVAL=1;
  118. FVLINENO=FPLINENO = PLINENO = 0;VLINENO = -1;
  119. BOTTOM = PLVAL - M3VAL - M4VAL;
  120. OUTW=OUTPOS=OUTTOP=OUTBOT=OLDLN=OLDBOT=OUTWRDS = 0;
  121. OUTBUF [0] = '\0';
  122. DIR = 0;
  123. EH2 = EH3 = EHEAD ;
  124. OH2 = OH3 = OHEAD ;
  125. *EHEAD = *OHEAD = '\0' ;
  126. EF2 = EF3 = EFOOT ;
  127. OF2 = OF3 = OFOOT ;
  128. *EFOOT = *OFOOT = '\0' ;
  129. setmem(CPTR,2*(128-' '),0);
  130. setmem(TPTR,2*(128-' '),0);
  131. TREND = TRTBL;
  132. OUTBUF2[0]=BPOS=0;
  133. initxu();
  134. MCNT=1;
  135. UF=XF=FALSE;
  136. setmem(&DBUF,LSZ,FALSE);
  137. DPOS=-1;
  138. FPTR=DLINK=RLINK=SLINK=MLINK=0;
  139. KPTR=KLINE;
  140. *KLINE=0;
  141. KEYBD=FALSE;
  142. rsvstk(2000);
  143. _allocp = NULL; /*needed for alloc() and free()*/
  144. IOBUF = alloc(BUFSIZ);
  145. complete();
  146. }
  147. /****************************************/
  148.  
  149. /*    July 1: \#\ replaced by current page number.
  150.     June 8: concatenate lines with trailing '\'
  151.     June 1, 1983 modified for macro string processing.
  152.     fgets2:    (May 2, 1983 by EEB fix to reset parity)
  153.     This next function is like "gets", except that
  154.     a) the line is taken from a buffered input file instead
  155.     of from the console, and b) the newline is INCLUDED in
  156.     the string and followed by a null byte. 
  157.     
  158.     This one is a little tricky due to the CP/M convention
  159.     of having a carriage-return AND a linefeed character
  160.     at the end of every text line. In order to make text
  161.     easier to deal with from C programs, ngetc()
  162.     automatically strips off the CR from any CR-LF
  163.     combinations that come in from the file. Any CR
  164.     characters not immediately followed by LF are left
  165.     intact. The LF is included as part of the string, and
  166.     is followed by a null byte. (Note that LF equals
  167.     "newline".) There is no limit to how long a line
  168.     can be here; care should be taken to make sure the
  169.     string pointer passed to fgets points to an area
  170.     large enough to accept any possible line length
  171.     (a line must be terminated by a newline (LF, or '\n')
  172.     character before it is considered complete.)  The
  173.     ngetc() also resets the parity bit.
  174.  
  175.     The value NULL (defined to be 0 here) is returned
  176.     on EOF, whether it be a physical EOF (attempting to
  177.     read past last sector of the file) OR a logical EOF
  178.     (encountered a control-Z.) The 1.3 version didn't
  179.     recognize logical EOFs, because I did't realize how
  180.     SIMPLE it was to implement a buffered I/O "ungetc"
  181.     function.
  182. */
  183.  
  184. char *fgets2(s,iobuf)
  185. char *s;
  186. struct _buf *iobuf;
  187. {
  188.     int count,c,i,*pw,pnum;
  189.     char *cptr,*pc;
  190.     char wbuf[LSZ],*fnd;
  191.     count = MAXLINE;
  192.     cptr = s;
  193.     if ( (c=ngetc(iobuf))==CPMEOF || c==EOF)
  194.         return NULL;
  195.  
  196.     do {    if(c==ICVAL)/*need macro substitution*/
  197.           {for(i=0;ICVAL!=(wbuf[i]=ngetc(iobuf));i++)
  198.             {if(wbuf[i]=='\n')
  199.                 {if(i) putback('\n');
  200.                 break;
  201.                 }
  202.             }
  203.           if(i) 
  204.            {wbuf[i]='\0';
  205.             if((*wbuf==NUMSIGN)&&(i==1))
  206.             {if((VLINENO>=PLVAL)
  207.                 ||(VLINENO<0)) pnum=NEWPAG;
  208.             else pnum=CURPAG;
  209.             itoc(pnum,wbuf,10);
  210.             pbstr(wbuf);
  211.             }
  212.             else if(pw=find2(wbuf,RLINK))
  213.             {itoc(*pw,wbuf,10);
  214.             pbstr(wbuf);
  215.             }
  216.             else if(fnd=find2(wbuf,SLINK)) pbstr(fnd);
  217.             else{start();
  218.                 pc=wbuf;
  219.             pw=TREND;
  220.             *(pw++)=SLINK;
  221.             transfer(&pc,&pw,'\0');
  222.             fprintf(STDERR,"%cPlease define <%s>:",
  223.                         BELL,wbuf);
  224.             gets(wbuf);
  225.             pc=wbuf;
  226.             transfer(&pc,&pw,'\0');
  227.             SLINK=TREND;
  228.             TREND=pw;
  229.             complete();
  230.             pbstr(wbuf);
  231.             }
  232.             continue;
  233.             }
  234.            else if(*wbuf!='\n') putback(ICVAL); 
  235.            c=ngetc(iobuf);
  236.           }
  237.         if ((*cptr++ = c) == '\n') break;
  238.  
  239.      } while (count--&&(c=ngetc(iobuf))!=EOF&&c!=CPMEOF);
  240.  
  241.     if(c==CPMEOF)putback(c);    /*push back control-Z*/
  242.     *cptr = '\0';
  243.     return s;
  244. }
  245. /**************************************************************
  246. performs the formatting command returned by comtyp -sets global
  247.   variables ( indenting, underlining, etc. )
  248. **************************************************************/
  249. comand ( line )
  250. char *line;
  251. {int c_type;    /* command type  */
  252. int arg_val;    /* argument value, if any */
  253. char arg_typ;    /* relative (+ or -) or absolute */
  254. char wbuf[20];
  255. int i;
  256. c_type = comtyp (line);
  257. if DEBUG fprintf(STDERR,"COMAND %d,",c_type);
  258. if (c_type == UNKNOWN)
  259.     {fprintf(STDERR, "UNKNOWN COMMAND: <%s>\n", line);
  260.     return;
  261.     }
  262. arg_val = get_val ( line, &arg_typ );
  263. if DEBUG
  264. fprintf(STDERR,"get_val returned arg_val=%d,arg_typ=%c\n",
  265.                    arg_val,    arg_typ   );
  266. switch (c_type)
  267.     {case IG : break;/* ignore remark */
  268.  
  269.     case FI :    /* filled lines  */
  270.         brk();
  271.         FILL = YES;
  272.         break;
  273.  
  274.     case NF :    /* non-filled lines */
  275.         brk();
  276.         FILL = NO;
  277.         break;
  278.  
  279.     case NJ :    /* non-justified lines */
  280.         JUSTIFY = NO;
  281.         break;
  282.  
  283.     case JU :    /* justified lines  */
  284.         JUSTIFY = YES;
  285.         break;
  286.  
  287.     case BR :    /* just cause a break */
  288.         brk();
  289.         break;
  290.  
  291.     case LS :    /* set line spacing value */
  292.         setS(&LSVAL,